let {
  api_GetExchangeInfo
} = require("../../api/getData.js")

// import {
//   $wuxToptips
// } from '/pages/components/wux-index'

const app = getApp()

Page({
  data: {
    // text:"这是一个页面"
    hidden: false,
    //搜索名称
    searchName: null,
    //pageindex
    pageIdx: 1,

    //模态框
    hideModal: true, //模态框的状态  true-隐藏  false-显示
    animationData: {},

    swiperList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    previousMargin: 0,
    nextMargin: 0,

    //页面数据
    productArr: [],
    clientHeight: 0,
    arr: [],
    arrHight: [],
    yisen_cost: 60,
    glGoods:[],
    color_choose: "",
    goods_cnt: 0,
    yisen_count: 0,
    yisen_cost: 0,
    userInfo: [],
    goodsList: [],
    userid:"",
    color_index:0,
    maxQuantity: 1,
    endTime: "",
    hour: 0,
    min: 0,
    sec: 0,
    ms: 0,
  },

  onLoad: function (options) {

    console.log("home页面被加载了")
    this.setData({
      swiperList: [{
        url: '',
        img: '/images/swipper/1.jpg',
        title: '学院路校区'
      }, {
        url: '',
          img: '/images/swipper/2.jpg',
        title: '图书馆'
      }, {
        url: '',
          img: '/images/swipper/3.jpg',
        title: '沙河校区'
      }]
    })
    var data_post = { 'userid': app.globalData.userInfo.id}
    api_GetExchangeInfo(data_post,(res) => {
      var tmpList = []
      res.data.goodsInfo.forEach(i => {
        var tmp = i;
        tmp.color_list_new = i.color_list.split(";");
        tmp.kucun_list_new = i.kucun_list.split(";");
        tmpList.push(tmp)
      })
      this.setData({
        goodsList: tmpList,
        userInfo: res.data.userInfo,
        endTime: tmpList[0].end_time
      })
      this.countDown();
    })
  },

  bindGoodsCnt: function(e) {
    if (isNaN(parseInt(e.detail.value))){
      this.setData({
        goods_cnt: 0,
        yisen_cost: 0
      })
    }else{
      this.setData({
        goods_cnt: e.detail.value,
        yisen_cost: e.detail.value * this.data.glGoods.cost_count,
      })
    }
    console.log('需花费伊森币'+this.data.yisen_cost)
  },

  setActFormMaxQuantity: function (e) {
    this.setData({
      goods_cnt: e.detail
    })
  },

  bindEnterExchange:function(){
    var dataTmp = JSON.stringify(this.data.userInfo)
    wx.navigateTo({
      url: '../exchange/exchange?exchangeUserInfo='+dataTmp,
      success: function (res) {

      },
      fail: function () {
       console.log("失败了！")
      },
    })
  },
  
  countDown(){
    let newTime = new Date().getTime();
    let endTime = new Date(this.data.endTime).getTime();
    let time = (endTime-newTime)/1000;
    let day = parseInt(time / (60 * 60 * 24)); 
    let hour = parseInt(time % (60 * 60 * 24) / 3600)+day*24; 
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60); 
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    let ms = 0;
    if(this.data.ms==0)
      ms = 9;
    else
      ms = this.data.ms-1;
    this.setData({
      hour:hour,
      min:min,
      sec:sec,
      ms:ms,
    })
    setTimeout(this.countDown, 100);
  },

  bindExchange: function () {
    // if (isNaN(parseInt(this.data.goods_cnt))){
    //   console.log("请输入整数");
    //   return;
    // }
    // if(this.data.yisen_cost>this.data.userInfo.yisen_cnt){
    //   console.log("伊森币不够");
    //   wx.showModal({
    //     title: '提示',
    //     content: '一森币不够',
    //     success: function (res) {
    //       if (res.confirm) {
    //       } else if (res.cancel) {
    //       }
    //     }
    //   })  
    //   return;
    // }
    // if (this.data.color_choose==""){
    //   console.log("还未选择颜色");
    //   wx.showModal({
    //     title: '提示',
    //     content: '还未选择颜色',
    //     success: function (res) {
    //       if (res.confirm) {
    //       } else if (res.cancel) {
    //       }
    //     }
    //   })
    //   return;
    // }
    if (this.data.goods_cnt==0) {
      console.log("请选择物品数量");
      wx.showModal({
       content: '请选择物品数量',
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
      return;
    }
    // if (this.data.goods_cnt > parseInt(this.data.glGoods.kucun_list_new[this.data.color_index])) {
    //   console.log("库存不够了");
    //   wx.showModal({
    //     content: '库存不够了',
    //     success: function (res) {
    //       if (res.confirm) {
    //       } else if (res.cancel) {
    //       }
    //     }
    //   })
    //   return;
    // }
    var tmp = this.data.glGoods;
    tmp.kucun_list_new[this.data.color_index] = parseInt(this.data.glGoods.kucun_list_new[this.data.color_index])-this.data.goods_cnt;
    var dataTmp = JSON.stringify(tmp);
    var dataTmp2 = JSON.stringify(this.data.userInfo);
    wx.navigateTo({
      url: '../order/order?goodsInfo='+dataTmp+'&goods_cnt='+this.data.goods_cnt+'&color_choose='+this.data.color_choose+'&userInfo='+dataTmp2,
      success: function (res) {
        // success
      },
      fail: function () {
        console.log("失败了！")
      },
    })
  },

  bindChooseColor: function (e) {
    var dataTmp = e.currentTarget.dataset.index;
    this.setData({
      color_choose: this.data.glGoods.color_list_new[dataTmp],
      color_index: dataTmp,
      goods_cnt:0,
    })
    this.setData({
      maxQuantity: parseInt(this.data.glGoods.kucun_list_new[this.data.color_index]) < this.data.userInfo.yisen_cnt / this.data.glGoods.cost_count ? parseInt(this.data.glGoods.kucun_list_new[this.data.color_index]) : parseInt(this.data.glGoods.kucun_list_new[this.data.color_index]),
    })
  },


  showModal: function (e) {
    var that = this;
    that.setData({
      hideModal: false,
      glGoods: e.currentTarget.dataset.index,
    })
    that.setData({
      color_choose: this.data.glGoods.color_list_new[0],
      color_index:0,
      goods_cnt:0
    })
    this.setData({
      maxQuantity: parseInt(this.data.glGoods.kucun_list_new[this.data.color_index]) < this.data.userInfo.yisen_cnt / this.data.glGoods.cost_count ? parseInt(this.data.glGoods.kucun_list_new[this.data.color_index]) : parseInt(this.data.glGoods.kucun_list_new[this.data.color_index])
    })
    var animation = wx.createAnimation({
      duration: 600,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn();//调用显示动画
    }, 200)
  },

  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function () {
      that.setData({
        hideModal: true
      })
    }, 720)//先执行下滑动画，再隐藏模块

  },

  //动画集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },

})